<template>
  <div class="login-form">
  <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">
    <div align="center">
      <h1>您 好，请 注 册</h1>
    </div>
    <a-form-model-item has-feedback label="用户名" prop="user">
      <a-input v-model="ruleForm.user" />
    </a-form-model-item>
    <a-form-model-item has-feedback label="密码" prop="pass">
      <a-input v-model="ruleForm.pass" type="password" autocomplete="off" />
    </a-form-model-item>
    <a-form-model-item has-feedback label="确认密码" prop="checkPass">
      <a-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
    </a-form-model-item>
    <a-form-model-item has-feedback label="验证码" prop="validateCode">
      <a-input v-model.number="ruleForm.validateCode" />
    </a-form-model-item>
    <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
      <a-button type="primary" @click="submitForm('ruleForm')">
        立 即 注 册
      </a-button>
      <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">
        重 置
      </a-button>
    </a-form-model-item>
  </a-form-model>
  </div>
</template>
<script>
export default {
  data() {
    let checkPending;
    let checkCode = (rule, value, callback) => {
      clearTimeout(checkPending);
      if (!value) {
        return callback(new Error('Please input the age'));
      }
      checkPending = setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('Please input digits'));
        } else {
          if (value !== 6666) {
            callback(new Error('请输入验证码：6666'));
          } else {
            callback();
          }
        }
      }, 500);
    };
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value.length < 8 || value.length > 18){
        callback(new Error('密码长度在 8 到 16 之间'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    let checkUSer = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      }
      if (value.length < 3 || value.length > 4) {
        callback(new Error("请输入一个用户名的长度在 3 到 5 之间"));
      }
      callback();
    };
    return {
      ruleForm: {
        user: '',
        pass: '',
        checkPass: '',
        validateCode: '',
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'change' }],
        checkPass: [{ validator: validatePass2, trigger: 'change' }],
        validateCode: [{ validator: checkCode, trigger: 'change' }],
        user: [{ validator: checkUSer, trigger: 'change' }],
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 },
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.registerUserInfo();
        } else {
          alert('校验失败请检查!');
          console.log('校验失败请检查!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    registerUserInfo() {
      this.$axios.post("http://localhost:8888/register", {
        username: this.ruleForm.user,
        password: this.ruleForm.pass
      }).then((response) => {
        if (response.data.code === 200) {
          // 注册成功
          this.$message.success("恭喜您，注册成功", 1.0);
          setTimeout(() => {
            this.$router.push("/login");
          }, 2000);
        }
        if (response.data.code === 500) {
          // 注册失败
          console.log(response.data.msg);
          this.$refs.ruleForm.resetFields();
          this.$message.success(response.data.msg, 2.0);
        }
      }).catch(error => {
        console.log(error);
      })
    }
  },
};
</script>
<style>
#components-form-demo-normal-login .login-form {
  max-width: 300px;
}

#components-form-demo-normal-login .login-form-forgot {
  float: right;
}

#components-form-demo-normal-login .login-form-button {
  width: 100%;
}

.login-form {
  width: 500px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
